<?php

class Db_User extends Model_Db_Core {

	protected  $con       = null;
	protected  $user   = null;
	protected  $user_id   = null;
	protected  $value   = array();

	function __construct(){
	}
	public function LoginCheck($email, $password) {	    
	    $filter  = array('email'=> $email, 'passwort' => md5($password));
	    $select  = array("id","username");
	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $user = parent::result(1); 
	    
		return $user;
	}
	public function GetUserByID($ID) {
	    $filter  = array('id'=> $ID);
	    $select  = array();
	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $user = parent::result(1);
	    
		return $user;
	}
	public function GetUserByName($Name) {
	    $filter  = array('username'=> $Name);
	    $select  = array('id','username','aktiv');
	    	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $user = parent::result(1);
	    
		return $user;
	}

	public function CreateUser($request){
		parent::setInsert(__CLASS__);
			
		$this->setValue($request);
		$this->value['aktiv'] = $this->gen_unique_id(NULL,time(),20);
				
	    $status = $this->writeToDb();	
	    
	    if($status)
	    {
	        $con = self::startGDB();
            $con->Select('INSERT INTO user_session (`id` ,`user_id` ,`date`)VALUES (NULL ,  "'.$this->getLastInsertId($status).'",CURRENT_TIMESTAMP)');
	    }    
	    return $status;
	}

    function gen_unique_id($id = NULL, $salt = NULL, $length = NULL){
        $id = ($id == NULL) ? uniqid(hash("sha512",rand()), TRUE) : $id;
        $code = hash("sha512", $id.$salt);
        return $length == NULL ? $code : substr($code, 0, $length);
    }
	public function setValue($request){
	    if(array_key_exists('username',$request)){
	        $this->value['username'] = $request['username'];
	    }
	    if(array_key_exists('email',$request)){
	        $this->value['email'] = $request['email'];
	    }
	    if(array_key_exists('passwort',$request)){
	        $this->value['passwort'] = md5($request['passwort']);
	    }
	    if(array_key_exists('age',$request)){
	        $this->value['age'] = $request['age'];
	    }
	    $this->value['reg_date'] = 'CURRENT_TIMESTAMP';
	}   
  
}

?>